package cn.hyq.basic.service.impl;

import cn.hyq.basic.mapper.BaseMapper;
import cn.hyq.basic.query.BaseQuery;
import cn.hyq.basic.service.IBaseService;
import cn.hyq.basic.util.PageList;
import cn.hyq.org.domain.Department;
import cn.hyq.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T>  implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    /**
     * 新增
     * @param t
     */
    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    /**
     * 修改
     * @param t
     */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void removeById(Long id) {
        baseMapper.delete(id);
    }

    /**
     * 基于id查询
     * @param id
     * @return
     */
    @Override
    public T queryById(Long id) {

        return baseMapper.loadById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<T> queryAll() {
        return baseMapper.loadAll();
    }

    /**
     * 高级查询-分页
     * @param query
     * @return
     */
    @Override
    public PageList<T> queryPage(BaseQuery query) {


        //查询总条数
        Long total = baseMapper.queryCount(query);
        //查询所有数据
        List<T> rows = baseMapper.queryData(query);

        return new PageList<T>(total,rows);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void batchRemoveById(List<Long> ids) {

        baseMapper.batchRemoveById(ids);
    }
}
